package com.dinadi.yidi.ui.fragment;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.CommodityAdapter;
import com.dinadi.yidi.base.BaseFragment;
import com.dinadi.yidi.bean.CreativeProductBean;
import com.dinadi.yidi.bean.CreativeProductRequest;
import com.dinadi.yidi.bean.CreativeProductRequestEntity;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;
import com.dinadi.yidi.utils.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * author: JiaYu.He
 * Date: 2017/9/6   综合商品
 * Time: 10:31
 */
public class ComprehensiveCommodityFragment2 extends BaseFragment {
    private View view;
    private PullLoadMoreRecyclerView mPullLoadMoreRecyclerView;
    private CommodityAdapter mRefreshAdapter;
    private List<CreativeProductRequestEntity> dataList = new ArrayList<>();
    private int page = 1;
    private String token, categoryId, sortType, minPrice, maxPrice;


    @Override
    protected void noWifiRefresh() {
        showLoadingDialog(getResources().getString(R.string.dialog_text));
        initPullrefresh();

    }

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_comprehensive_commodity, null);
        return view;
    }

    @Override
    protected void initView() {

        changePager(2);
        mPullLoadMoreRecyclerView = (PullLoadMoreRecyclerView) view.findViewById(R.id.pullLoadMoreRecyclerView);
        mPullLoadMoreRecyclerView.setGridLayout(2);
        mRefreshAdapter = new CommodityAdapter(getActivity());
        mPullLoadMoreRecyclerView.setAdapter(mRefreshAdapter);
        mPullLoadMoreRecyclerView.setOnPullLoadMoreListener(new PullLoadMoreListener());
        mPullLoadMoreRecyclerView.setPullRefreshEnable(false);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initListener() {


    }


    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {
            default:
                break;
        }

    }

    @Override
    protected void initData() {
        token = SharePreferencesHelper.getInstance(getActivity()).getString("token");
        categoryId = SharePreferencesHelper.getInstance(getActivity()).getString("productCategoryId");
        sortType = SharePreferencesHelper.getInstance(getActivity()).getString("sortType");
        minPrice = SharePreferencesHelper.getInstance(getActivity()).getString("lower_price");
        maxPrice = SharePreferencesHelper.getInstance(getActivity()).getString("higher_price");
        showLoadingDialog(getResources().getString(R.string.dialog_text));
        initPullrefresh();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    HttpListener httpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            changePager(2);
            closeLoadingDialog();

            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                CreativeProductRequest bean = JsonUtil.parseJsonToBean(response, CreativeProductRequest.class);
                dataList = bean.getProductList();
                mRefreshAdapter.addAllData(dataList);
                getData();


            } else {
                closeLoadingDialog();
                toast(errMsg);
            }


        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            changePager(1);
            closeLoadingDialog();
            toast("加载失败");
        }
    };

    private void getData() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                //刷新结束
                mPullLoadMoreRecyclerView.setPullLoadMoreCompleted();
            }
        }, 1000);
    }


    class PullLoadMoreListener implements PullLoadMoreRecyclerView.PullLoadMoreListener {
        @Override
        public void onRefresh() {
            setRefresh();
        }

        @Override
        public void onLoadMore() {
            page = page + 1;
            initPullrefresh();
        }
    }

    private void setRefresh() {
        mRefreshAdapter.clearData();
        page = 1;
        initPullrefresh();
    }

    private void initPullrefresh() {
        CreativeProductBean bean = new CreativeProductBean();
        bean.setPageNum(page + "");
        bean.setPageSize("10");
        if (categoryId != null) {
            bean.setCategoryId(categoryId);
        }
        bean.setSortType(sortType);
        bean.setMinPrice(minPrice);
        bean.setMaxPrice(maxPrice);
        CommonInterface creativeProductInterface = new CommonInterface(getActivity(), httpListener);
        creativeProductInterface.getInterface("13003", JSON.toJSONString(bean));
//        SharePreferencesHelper.getInstance(getActivity()).putString("productCategoryId",null);
    }

    public void onEventMainThread(CreativeProductBean event) {
        if (event != null) {
            mRefreshAdapter.clearData();
            page = 1;
            sortType = event.getSortType();
            minPrice = event.getMinPrice();
            maxPrice = event.getMaxPrice();
            categoryId = event.getCategoryId();
            showLoadingDialog(getResources().getString(R.string.dialog_text));
            initPullrefresh();
        } else {
            sortType = "";
        }

    }
}

